package net.bplaced.streetfighter.adapter;

import java.util.List;

import net.bplaced.streetfighter.R;
import net.bplaced.streetfighter.lib.model.Team;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.de>
 */
public class TeamAdapter
        extends AbstractAdapter<Team> {

    public TeamAdapter(final Context context, final List<Team> teams) {

        super(context, teams);
    }

    @Override
    View getView(int position, View convertView, LayoutInflater inflater) {

        final Team t = this.items.get(position);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_item_team, null);
        }

        // Rank field
        final TextView rankField = (TextView) convertView.findViewById(R.id.team_table_adapter_item_rank);
        // Status field
        final TextView statusField = (TextView) convertView.findViewById(R.id.team_table_adapter_item_status);
        // Name field
        final TextView nameField = (TextView) convertView.findViewById(R.id.team_table_adapter_item_name);
        // Matches played
        final TextView matchesPlayedField =
                (TextView) convertView.findViewById(R.id.team_table_adapter_item_matches_played);
        // Matches won
        final TextView matchesWonField = (TextView) convertView.findViewById(R.id.team_table_adapter_item_matches_won);
        // Matches draw
        final TextView matchesDrawField =
                (TextView) convertView.findViewById(R.id.team_table_adapter_item_matches_draw);
        // Matches lost
        final TextView matchesLostField =
                (TextView) convertView.findViewById(R.id.team_table_adapter_item_matches_lost);
        // Goal percentage
        final TextView goalPercentageField =
                (TextView) convertView.findViewById(R.id.team_table_adapter_item_goal_percentage);
        // Goal difference
        final TextView goalDifferenceField =
                (TextView) convertView.findViewById(R.id.team_table_adapter_item_goal_difference);
        // Points
        final TextView pointsField = (TextView) convertView.findViewById(R.id.team_table_adapter_item_points);

        if (position == 0) {

            rankField.setText("Pos.");
            rankField.setTextSize(13);
            statusField.setText(null);
            nameField.setText("Team");
            nameField.setTypeface(Typeface.DEFAULT_BOLD);
            matchesPlayedField.setText("Sp.");
            matchesPlayedField.setTypeface(Typeface.DEFAULT_BOLD);
            matchesWonField.setText("S");
            matchesWonField.setTypeface(Typeface.DEFAULT_BOLD);
            matchesDrawField.setText("U");
            matchesDrawField.setTypeface(Typeface.DEFAULT_BOLD);
            matchesLostField.setText("N");
            matchesLostField.setTypeface(Typeface.DEFAULT_BOLD);
            goalPercentageField.setText("Tore");
            goalPercentageField.setTypeface(Typeface.DEFAULT_BOLD);
            goalDifferenceField.setText("Diff");
            goalDifferenceField.setTypeface(Typeface.DEFAULT_BOLD);
            pointsField.setText("Pkt.");
            pointsField.setTypeface(Typeface.DEFAULT_BOLD);

        } else {

            rankField.setText(t.rank.toString());

            String statusString = "";

            // raising
            if (t.status.equals("&#8593;")) {
                statusString = "▲";
                // alternating
            } else if (t.status.equals("&#8226;")) {
                statusString = "•";
                // falling
            } else if (t.status.equals("&#8595;")) {
                statusString = "▼";
            }
            statusField.setText(statusString);

            nameField.setText(t.name);
            matchesPlayedField.setText(t.getMatchesPlayed().toString());
            matchesWonField.setText(t.matchesWon.toString());
            matchesDrawField.setText(t.matchesDraw.toString());
            matchesLostField.setText(t.matchesLost.toString());
            goalPercentageField.setText(t.getGoalPercentage());
            goalDifferenceField.setText(t.getGoalDiff().toString());
            pointsField.setText(t.getPoints().toString());
        }

        return convertView;
    }
}